package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.LableBean;
import com.cloud.dao.MyPatientBean;
import com.cloud.dao.PatientBean;
import com.cloud.service.LableService;
import com.cloud.service.MyPatientService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-12-9
 * @version 1.0 
 */
@Controller
@RequestMapping("/lable")
public class LableServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(LableServlet.class);
	@Autowired
	private LableService LabletService;
	@Autowired
	private MyPatientService MyPatientService;
	
	/**
	 * 查询标签信息
	 * @param request
	 * @param lable
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, LableBean lable) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(lable == null || (CommUtil.isEmpty(lable.getDoc_id()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始查询医生[%s]的标签信息", lable.getDoc_id()));
		List<LableBean> retLableList = new ArrayList<LableBean>();
		retLableList = LabletService.getLableList(lable);
		List<Map<String, String>> retMapList = new ArrayList<Map<String, String>>();
		for(LableBean _lable : retLableList){
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("id", _lable.getId());
			_map.put("name", _lable.getName());
			_map.put("groupid", _lable.getGroup_id());
			_map.put("createTime", _lable.getCreatetime());
			retMapList.add(_map);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retMapList);
		return map;
	}
	
	/**
	 * 添加标签
	 * @param request
	 * @param lable
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	public @ResponseBody Object addlable(HttpServletRequest request, @RequestBody LableBean lable) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(lable == null || CommUtil.isEmpty(lable.getDoc_id()) || CommUtil.isEmpty(lable.getGroup_id()) || CommUtil.isEmpty(lable.getName()) || CommUtil.isEmpty(lable.getId())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		String ids = lable.getId();
		String lable_id = LabletService.addLable(lable);
		for(String pat_id : ids.split(",")){
			MyPatientBean patient = new MyPatientBean();
			patient.setDoc_id(lable.getDoc_id());
			patient.setPat_id(pat_id);
			patient.setLable_id(lable_id);
			LabletService.addLablePatient(patient);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 删除标签
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/{doc_id}/delete/{lable_id}", method = { RequestMethod.POST})
	public @ResponseBody Object deletelablebyid(HttpServletRequest request, @PathVariable String doc_id, @PathVariable String lable_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(doc_id) || CommUtil.isEmpty(lable_id)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		LableBean lable = new LableBean();
		lable.setDoc_id(doc_id);
		lable.setId(lable_id);
		LabletService.deleteLable(lable);
		MyPatientBean mypatient = new MyPatientBean();
		mypatient.setDoc_id(doc_id);
		mypatient.setLable_id(lable_id);
		LabletService.deletePatientLable(mypatient);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 删除我的患者标签
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/delete", method = { RequestMethod.POST})
	public @ResponseBody Object deletelable(HttpServletRequest request, @RequestBody MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || CommUtil.isEmpty(mypatient.getDoc_id()) || CommUtil.isEmpty(mypatient.getPat_id()) || CommUtil.isEmpty(mypatient.getLable_id())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		LabletService.deletePatientLable(mypatient);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 添加某个患者标签
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/add/patientid", method = { RequestMethod.POST})
	public @ResponseBody Object addpatient(HttpServletRequest request, @RequestBody MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || CommUtil.isEmpty(mypatient.getDoc_id()) || CommUtil.isEmpty(mypatient.getPat_id()) || CommUtil.isEmpty(mypatient.getLable_id())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		String lables = mypatient.getLable_id();
		for(String _labId : lables.split(",")){
			mypatient.setLable_id(_labId);
			LabletService.addLablePatient(mypatient);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 查询我的患者标签
	 * @param request
	 * @param doctorid
	 * @param patientid
	 * @return
	 */
	@RequestMapping(value = "/{doctorid}/{patientid}/list", method = { RequestMethod.GET})
	public @ResponseBody Object qpatientlable(HttpServletRequest request, @PathVariable String doctorid, @PathVariable String patientid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(doctorid) || CommUtil.isEmpty(patientid)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		MyPatientBean mypatient = new MyPatientBean();
		mypatient.setDoc_id(doctorid);
		mypatient.setPat_id(patientid);
		List<LableBean> lableList = LabletService.getMyPatientLableList(mypatient);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", lableList);
		return map;
	}
	
	/**
	 * 查询标签下的我的患者
	 * @param request
	 * @param doctorid
	 * @param lableid
	 * @return
	 */
	@RequestMapping(value = "/{doctorid}/{lableid}/patient", method = { RequestMethod.GET})
	public @ResponseBody Object qlablepatient(HttpServletRequest request, @PathVariable String doctorid, @PathVariable String lableid) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(doctorid) || CommUtil.isEmpty(lableid)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		LableBean lable = new LableBean();
		lable.setDoc_id(doctorid);
		lable.setId(lableid);
		List<PatientBean> lableList = MyPatientService.getMyPatientListByLable(lable);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", lableList);
		return map;
	}
	
	/**
	 * 修改标签
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	public @ResponseBody Object update(HttpServletRequest request, @RequestBody LableBean lable) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(lable == null || CommUtil.isEmpty(lable.getDoc_id()) || CommUtil.isEmpty(lable.getId()) || (CommUtil.isEmpty(lable.getPat_id()) && CommUtil.isEmpty(lable.getGroup_id()) && CommUtil.isEmpty(lable.getName()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		if(!CommUtil.isEmpty(lable.getGroup_id()) || !CommUtil.isEmpty(lable.getName())){
			LabletService.updateLable(lable);
		}
		if(!CommUtil.isEmpty(lable.getPat_id())){
			MyPatientBean mypatient = new MyPatientBean();
			mypatient.setDoc_id(lable.getDoc_id());
			mypatient.setLable_id(lable.getId());
			LabletService.deletePatientLable(mypatient);
			for(String pid : lable.getPat_id().split(",")){
				mypatient.setPat_id(pid);
				LabletService.addLablePatient(mypatient);
			}
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
